import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.helpers.DefaultHandler;


public class ParSax {
SAXParserFactory fac;
SAXParser parser;
String grupa=new String("grupa");
String nazwa=new String("nazwa");
String cena=new String("cena");
FileOutputStream stream;
	public ParSax(String source, String result){
		fac= SAXParserFactory.newInstance();
		try {
			parser = fac.newSAXParser();
			File f=new File(source);
			File z =new File(result);
			f.createNewFile();
			 stream =new FileOutputStream(z);
			String s=new String("costam");
			
			DefaultHandler h = createHendler();
			parser.parse(f, h);
			
			
			
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private DefaultHandler createHendler() {
		DefaultHandler hend= new DefaultHandler(){


			public void characters(char[] ch, int start, int length)
					throws SAXException {
				String s=new String();
				for(int i=0;i<length;++i)
					s=s+(ch[start+i]+"");
				try {
					p(s.trim());
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				//p("\t");
				super.characters(ch, start, length);
			}


			public void endDocument() throws SAXException {
				// TODO Auto-generated method stub
				super.endDocument();
			}


			public void endElement(String uri, String localName, String qName)
					throws SAXException {
				//p("-"+qName+"  ");
				if(qName.equals(new String("produkt")))
					try {
						pln();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				super.endElement(uri, localName, qName);
			}


			public void startDocument() throws SAXException {
				// TODO Auto-generated method stub
				super.startDocument();
			}


			public void startElement(String uri, String localName,
					String qName, Attributes attributes) throws SAXException {
				
				if(qName.equals(grupa))
					try {
						pln();
						pl(attributes.getValue(0));
						
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				else if(qName.equals(nazwa))
					try {
						p("\tnazwa: ");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				else if(qName.equals(cena))
					try {
						p("\tcena: ");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
		
				super.startElement(uri, localName, qName, attributes);
			}
			
		};
		return hend;
	}/*
	private void pt(Object o){
		System.out.print(o.toString());
	}
	private void ptl(Object o){
		System.out.println(o.toString());
	}*/
	private void pl(String s) throws IOException{
		System.out.println(s);
		s=s+"\n";
		stream.write(s.getBytes());
	}
	private void p(String s) throws IOException{
		System.out.print(s);
		stream.write(s.getBytes());
	}
	private void pln() throws IOException{
		System.out.println("");
		String s= new String("\n");
		stream.write(s.getBytes());
	}
}
